<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>业务员</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户信息</label>
                            <div class="layui-input-inline">
                                <input type="text" name="info" placeholder="请输入用户昵称或手机号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="update">更新二维码</button>
                    </div>
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="name">
                        <div style="display: flex">
                            <img class="imgs" src="{{d.portrait}}" title="{{d.name}}" style="width: 40px;height: 40px;cursor: pointer;">
                            <div style="margin-left: 15px;">
                                <div style="flex-wrap: wrap;">{{d.name}}</div>
                            </div>
                        </div>
                    </script>
                    <script type="text/html" id="gold">
                        {{ returnFloat(d.gold) }} ¥
                    </script>
                    <script type="text/html" id="time">
                        {{ formatDateTime(d.time) }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        <!--<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="look"><i class="layui-icon layui-icon-username"></i>查看下级</a>-->
                    </script>
                </div>
            </div>
        </div>
    </fieldset>

</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$,
                form = layui.form,
                table = layui.table;

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                url: "{:url('User/business')}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('User/business')}",
            method: 'post',
            cols: [[
                {field: 'name', title: '用户昵称', templet: "#name"},
                {field: 'phone', title: '用户手机'},
                {field: 'openid', title: '微信openid'},
                {field: 'gold', title: '佣金余额', templet: "#gold"},
                {field: 'time', title: '加入时间', templet: "#time"},
                {fixed: 'right', title: '操作', width: 250, align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if(layEvent === 'look'){ //编辑
                layer.open({
                    type: 2
                    ,title: '下级会员'
                    ,content: '/admin/User/lower/id/' + id
                    ,maxmin: true
                    ,area: ['100%', '100%']
                    ,btn: '关闭'
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        layer.close(index);
                    }
                });
            }
        });

        //事件
        var active = {
            add: function(){
                var index = layer.load(1, {offset: ['30%', '42%']});
                $.post("{:url('User/business_add')}", {}, function(e){
                    layer.close(index);
                    var img = e.img;
                    layer.open({
                        type: 1,
                        title: '添加业务员',
                        closeBtn: 2,
                        area: ['auto', 'auto'],
                        offset: ['20%', '35%'],
                        btn: '关闭',
                        btnAlign: 'c',
                        content: '<div style="padding: 30px 50px;background: #b9b9b9;"><img style="width: 200px;height: 200px;" src="/'+img+'"></div>'
                    });
                }, 'json');
            },
            update: function(){
                layer.confirm('<div>1.更新前需要在 系统设置-微信支付设置 <span style="color: red;">更新你的小程序账号和秘钥</span></div>' +
                        '<div>2.更新成功后<span style="color: red;">二维码没加载请清除浏览器缓存刷新重试</span></div>', {
                    title: '更新提示',
                    offset: ['25%', '35%'],
                    area: '200px',
                    btn: ['确认更新', '关闭']
                }, function(index){
                    var indexs = layer.load(1, {offset: ['30%', '42%']});
                    $.post("{:url('User/business_edit')}", {}, function(e){
                        layer.close(indexs);
                        if (e == 1){
                            layer.msg('更新成功', {offset: ['35%', '40%']});
                        }else{
                            layer.msg('更新失败', {offset: ['35%', '40%']});
                        }
                    }, 'json');
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }

    //补零
    function returnFloat(value){
        value = Math.round(parseFloat(value)*100)/100;
        var xsd = value.toString().split(".");
        if(xsd.length == 1){
            value = value.toString()+".00";
            return value;
        }
        if(xsd.length>1){
            if(xsd[1].length<2){
                value = value.toString()+"0";
            }
            return value;
        }
    }
</script>
</body>
</html>
